package org.cliff.message.platform.management.controller;

import org.cliff.message.platform.core.exception.MPBusinessException;
import org.cliff.message.platform.core.service.ReceivedMessageService;
import org.cliff.message.platform.management.domain.MPR;
import org.cliff.message.platform.management.dto.MarkMessageResendRequest;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 接收/消费消息重发控制器
 * @author 刘文
 * @date 2023/10/27
 */
@Api("接收/消费消息重发控制器")
@RestController
@RequestMapping("/received-message/v1")
public class ReceivedMessageResendController {

    @Autowired
    private ReceivedMessageService receivedMessageService;


    @PostMapping("/resend-messages")
    @ApiOperation("将指定消息标记为重发")
    public MPR<Boolean> markMessageResend(@RequestBody @Valid MarkMessageResendRequest resendRequest) {
        // 默认10分钟
        long delayTimeInMillis = 0;
        if (MarkMessageResendRequest.DELAY_TYPE_TEN_MINUTES.equalsIgnoreCase(resendRequest.getDelayType())) {
            delayTimeInMillis = 10 * 60 * 1000;
        } else if (MarkMessageResendRequest.DELAY_TYPE_SPECIFIED.equalsIgnoreCase(resendRequest.getDelayType())) {
            if (resendRequest.getSpecifiedDelayInMinutes() == null) {
                throw new MPBusinessException("用户指定延迟发送时间 不能为空");
            }
            delayTimeInMillis = resendRequest.getSpecifiedDelayInMinutes() * 60 * 1000;
        }
        receivedMessageService.markMessagesResend(resendRequest.getMessageIds(), delayTimeInMillis);
        return MPR.ok(Boolean.TRUE);
    }
}
